/**
 * 获取当前路径的?后面的参数,
 * 假如当前 Url 是 http//www.liangshunet.com/pub/item.aspx?t=osw7&id=100001234，
 * 获取id，则为：100001234
 * @param url
 * @returns {String}
 */
export const getUrlKey = (url, name) => {
    return decodeURIComponent(
        (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ""])[1].replace(/\+/g, '%20')) || null;
}

/**
 * 获取当前相对路径的方法,
 * 假如当前 Url 是 http//www.liangshunet.com/pub/item.aspx?t=osw7，
 * 则截取到的相对路径为：/pub/item.aspx。。
 * @param url
 * @returns {Array}
 */
export const getUrlRelativePath = (url) => {
    //        var url = document.location.toString();
    var arrUrl = url.split("//");
    var start;
    var relUrl;
    if (arrUrl.length === 2) {
        start = arrUrl[1].indexOf("/");
        relUrl = arrUrl[1].substring(start); //stop省略，截取从start开始到结尾的所有字符
    } else {
        start = arrUrl[0].indexOf("/");
        relUrl = arrUrl[0].substring(start); //stop省略，截取从start开始到结尾的所有字符
    }

    if (relUrl.indexOf("?") !== -1) {
        relUrl = relUrl.split("?")[0];
    }
    return relUrl.split("/");
}

/**
 * 获取当前URL所有参数的方法,
 * 假如当前 Url 是 http//www.liangshunet.com/pub/item.aspx?t=osw7&i=100，
 * 则截取到的所有参数为：[{key:t,value:osw7},{key:i,value:100}]
 * @param url
 * @returns {Array}
 */
export const getUrlAllParam = (url) => {
    var vars = url.split("?");
    if (vars.length === 2) {
        let params = [];
        let param = vars[1].split("&");
        for (var i = 0; i < param.length; i++) {
            var pair = param[i].split("=");
            params.push({
                key: pair[0],
                value: pair[1]
            });
        }
        return params;
    } else {
        return [];
    }
}